package au.edu.swin.swinvehicle.vehicletype;

/**
 * VehicleTypeManagerBean
 * @version 1.0
 * @author Viet Bui
 * @lastmodify 2009-05-11
 */
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class VehicleTypeManagerBean implements VehicleTypeManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addVehicleType(VehicleTypeDetailedTO to) throws VehicleTypeManagerException {
        try {
            VehicleTypeEntity vt = new VehicleTypeEntity();
            vt.setData(to);
            this.em.persist(vt);
        } catch (Exception e) {
            throw new VehicleTypeManagerException("Cannot add vehicle type", e);
        }
    }

    public void editVehicleType(VehicleTypeDetailedTO to) throws VehicleTypeManagerException {
        try {
            VehicleTypeEntity vt = new VehicleTypeEntity();
            vt.setData(to);
            this.em.merge(vt);
        } catch (Exception e) {
            throw new VehicleTypeManagerException("Cannot edit vehicle type", e);
        }
    }

    public void deleteVehicleType(Long id) throws VehicleTypeManagerException {
        try {
            VehicleTypeEntity vt = this.em.find(VehicleTypeEntity.class, id);
            vt.setDeleted(Boolean.TRUE);
            this.em.merge(vt);
        } catch (Exception e) {
            throw new VehicleTypeManagerException("Cannot delete vehicle type", e);
        }
    }

    public List<VehicleTypeTO> getAllVehicleTypes() throws VehicleTypeManagerException {
        try {
            List<VehicleTypeTO> results = new ArrayList<VehicleTypeTO>();
            Query allVehicleTypesQuery = this.em.createNamedQuery("findAllVehicleTypes");
            List dbResults = allVehicleTypesQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                VehicleTypeEntity vt = (VehicleTypeEntity) dbResults.get(i);
                results.add(vt.getData());
            }
            return results;
        } catch (Exception e) {
            throw new VehicleTypeManagerException("Cannot get all vehicle types", e);
        }
    }

    public List<VehicleTypeDetailedTO> getAllDetailedVehicleTypes() throws VehicleTypeManagerException {
        try {
            List<VehicleTypeDetailedTO> results = new ArrayList<VehicleTypeDetailedTO>();
            Query allVehicleTypesQuery = this.em.createNamedQuery("findAllVehicleTypes");
            List dbResults = allVehicleTypesQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                VehicleTypeEntity vt = (VehicleTypeEntity) dbResults.get(i);
                results.add(vt.getDetailedData());
            }
            return results;
        } catch (Exception e) {
            throw new VehicleTypeManagerException("Cannot get all detailed vehicle types", e);
        }
    }

    public VehicleTypeDetailedTO getVehicleType(Long id) throws VehicleTypeManagerException {
        try {
            VehicleTypeEntity vt = this.em.find(VehicleTypeEntity.class, id);
            return vt.getDetailedData();
        } catch (Exception e) {
            throw new VehicleTypeManagerException("Cannot get vehicle type by id " + id.longValue(), e);
        }
    }
}